package com.lipaluma.test.db.internal.assertions;

import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

/**
 * test a string column by a pattern
 * @author Mario
 */
class PatternColumn extends Column<String> {
	
	private PatternColumn(Builder builder) {
		super(builder);
	}
	
	public static PatternColumn.Builder builder(String request) {
		return new Builder(request);
	}

	@Override
	public void assertValue(ResultSet rs) throws SQLException {
		if(name == null)
			assertTrue(getErrorMessageOnIndexColumn()+"\nError on pattern, expected : "+expectedValue+" but was "+rs.getString(index), Pattern.matches(expectedValue, rs.getString(index)));
		else
			assertTrue(getErrorMessageOnNamedColumn()+"\nError on pattern, expected : "+expectedValue+" but was "+rs.getString(name), Pattern.matches(expectedValue, rs.getString(name)));
	}

	public static class Builder extends Column.Builder<String>  {

		public Builder(String request) {
			super(request);
		}

		@Override
		public PatternColumn.Builder withExpectedValue(String expected) {
			this.expectedValue = expected;
			return this;
		}

		@Override
		public PatternColumn build() {
			return new PatternColumn(this);
		}

	}


}
